//
//  ChangePwdViewController.m
//  taishan
//
//  Created by mac on 2019/4/16.
//  Copyright © 2019年 jznrj. All rights reserved.
//

#import "ChangePwdViewController.h"
#import "ImageControl.h"
#import <Masonry.h>
#import "SizeUtil.h"
#import "ColorUtil.h"
#import "LoginDto.h"
#import <MJExtension.h>
int preTag ;
float preMoveY;

const float loginChangeFont = 13;
const float titleChangeFont = 15;

@interface ChangePwdViewController ()

@property(nonatomic, strong) UILabel *loginLabel;
@property(nonatomic, strong) UILabel *userNameLabel;

@property(nonatomic, strong) UILabel *pwdLabel;
@property(nonatomic, strong) UITextField *pwdTextField;

@property(nonatomic, strong) UILabel *comfirmPwdLabel;
@property(nonatomic, strong) UITextField *comfirmPwdTextField;

@property(nonatomic, strong) UILabel *loginTitleLabel;

@property(nonatomic, strong) UIView *contentView;

@property(nonatomic, strong) UIView *topView;

@property(nonatomic, strong) UIView *middleView;

@property(nonatomic, strong) UIView *bottomView;

@property(nonatomic, strong) UIImageView *warningImage;

@property(nonatomic, strong) ImageControl *closeControl;

@property(nonatomic, strong) UIControl *backgroundControl;

@property(nonatomic, strong) UIButton *loginButton;

@end

@implementation ChangePwdViewController

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void) viewDidLoad{
    
    [self setupView];
}

-(void) setupView{
    [self.view addSubview:self.backgroundControl];
    [self.backgroundControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self.view);
    }];
    
    [self.view addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view).multipliedBy(0.8);
        make.height.equalTo(self.contentView.mas_width);
        make.top.equalTo(self.view).offset(100);
        make.centerX.equalTo(self.view);
    }];
    
    [self.contentView addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.contentView);
        make.width.equalTo(self.contentView);
        make.height.equalTo(self.contentView).multipliedBy(0.2);
    }];
    
    [self.topView addSubview:self.loginTitleLabel];
    [self.loginTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.topView);
    }];
    
    
    [self.contentView addSubview:self.middleView];
    [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(-8);
        make.left.equalTo(self.contentView);
        make.width.equalTo(self.contentView);
        make.height.equalTo(self.contentView).multipliedBy(0.4);
    }];
    
    [self.contentView addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.equalTo(self.contentView);
        make.width.equalTo(self.contentView);
        make.height.equalTo(self.contentView).multipliedBy(0.3);
    }];
    
    
    
    
    [self.middleView addSubview:self.loginLabel];
    [self.loginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(self.middleView).multipliedBy(0.33);
        make.top.equalTo(self.middleView);
        make.width.mas_equalTo(70*LSCALE);
        make.left.equalTo(self.middleView).offset(10);
        
    }];
    
    [self.middleView addSubview:self.userNameLabel];
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.loginLabel.mas_right);
        make.right.equalTo(self.middleView);
        make.top.bottom.equalTo(self.loginLabel);
    }];
    NSString *loginDtoJson = [[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
    LoginDto *loginDto = [LoginDto mj_objectWithKeyValues:loginDtoJson];
    self.userNameLabel.text = loginDto.userName;
    
    UIImageView *loginLine = [[UIImageView alloc]init];
    loginLine.backgroundColor = [UIColor darkGrayColor];
    [self.middleView addSubview:loginLine];
    [loginLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(@0.5);
        make.top.equalTo(self.loginLabel.mas_bottom);
        make.left.equalTo(self.middleView).offset(10);
        make.width.equalTo(self.middleView).offset(-20);
    }];
    
    [self.middleView addSubview:self.pwdLabel];
    [self.pwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.middleView).multipliedBy(0.33);
        make.width.mas_equalTo(70*LSCALE);
        make.top.equalTo(self.loginLabel.mas_bottom);
        make.left.equalTo(self.middleView).offset(10);
        
    }];
    
    [self.middleView addSubview:self.pwdTextField];
    [self.pwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.pwdLabel.mas_right);
        make.right.equalTo(self.middleView);
        make.top.bottom.equalTo(self.pwdLabel);
    }];
    

    
    UIImageView *pwdLine = [[UIImageView alloc]init];
    pwdLine.backgroundColor = [UIColor darkGrayColor];
    [self.middleView addSubview:pwdLine];
    [pwdLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0.5);
        make.top.equalTo(self.pwdLabel.mas_bottom);
        make.left.equalTo(self.middleView).offset(10);
        make.width.equalTo(self.middleView).offset(-20);
    }];
    
    [self.middleView addSubview:self.comfirmPwdLabel];
    [self.comfirmPwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.middleView).multipliedBy(0.33);
        make.width.mas_equalTo(70*LSCALE);
        make.top.equalTo(pwdLine.mas_bottom);
        make.left.equalTo(self.middleView).offset(10);
        
    }];
    
    [self.middleView addSubview:self.comfirmPwdTextField];
    [self.comfirmPwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.comfirmPwdLabel.mas_right);
        make.right.equalTo(self.middleView);
        make.top.bottom.equalTo(self.comfirmPwdLabel);
    }];
    
    UIImageView *comfirmPwdLine = [[UIImageView alloc]init];
    comfirmPwdLine.backgroundColor = [UIColor darkGrayColor];
    [self.middleView addSubview:comfirmPwdLine];
    [comfirmPwdLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0.5);
        make.top.equalTo(self.comfirmPwdLabel.mas_bottom);
        make.left.equalTo(self.middleView).offset(10);
        make.width.equalTo(self.middleView).offset(-20);
    }];
    
    [self.bottomView addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.bottomView).multipliedBy(0.4);
        make.width.equalTo(self.bottomView).multipliedBy(0.5);
        make.center.equalTo(self.bottomView);
    }];
    
    [self.view addSubview:self.closeControl];
    [self.closeControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40*LSCALE);
        make.width.mas_equalTo(40*LSCALE);
        make.bottom.equalTo(self.contentView.mas_top).offset(25*LSCALE);
        make.left.equalTo(self.contentView.mas_right).offset(-25*LSCALE);
    }];
}

-(void)changeLogin{
    
    
    if (self.pwdTextField.text == nil || self.pwdTextField.text.length == 0) {
        
        [self showMessage:@"请输入密码"];
        return;
    }
    if (self.comfirmPwdTextField.text == nil || ![self.comfirmPwdTextField.text isEqualToString: self.pwdTextField.text]) {
        
        [self showMessage:@"请输入相同密码"];
        return;
    }
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changePwd" object:nil userInfo:@{@"password":self.pwdTextField.text }];
}

-(void) removeSelf{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"removeChangeView" object:nil userInfo:nil ];
}

-(void) exitTextField{
    [self.pwdTextField resignFirstResponder];
    [self.comfirmPwdTextField resignFirstResponder];
}

-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    
    CGRect textFrame =  textField.frame;
    float textY = CGRectGetMaxY(textFrame)+textFrame.size.height;
    float bottomY = self.view.frame.size.height-textY-45;
    if(bottomY>=216)  //判断当前的高度是否已经有216，如果超过了就不需要再移动主界面的View高度
    {
        preTag = -1;
        return;
    }
    
    preTag = textField.tag;
    float moveY = 216-bottomY;
    preMoveY = moveY;
    
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    frame.origin.y -= moveY;//view的Y轴上移
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];//设置调整界面的动画效果
}


-(void) textFieldDidEndEditing:(UITextField *)textField
{
    if(preTag == -1) //当编辑的View不是需要移动的View
    {
        return;
    }
    float moveY ;
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    if(preTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
    {   //还原界面
        moveY =  preMoveY;
        frame.origin.y +=moveY;
    }
    //self.view移回原位置
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
    [textField resignFirstResponder];
    
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark ========懒加载===========

-(UILabel*) loginLabel{
    
    if (!_loginLabel) {
        _loginLabel = [[UILabel alloc]init];
        _loginLabel.font = [UIFont systemFontOfSize:loginChangeFont*LSCALE];
        _loginLabel.text = @"用户名：";
    }
    
    return _loginLabel;
}

-(UILabel*) pwdLabel{
    
    if (!_pwdLabel) {
        _pwdLabel = [[UILabel alloc]init];
        _pwdLabel.font = [UIFont systemFontOfSize:loginChangeFont*LSCALE];
        _pwdLabel.text = @"新密码：";
    }
    
    return _pwdLabel;
}

-(UILabel*) comfirmPwdLabel{
    
    if (!_comfirmPwdLabel) {
        _comfirmPwdLabel = [[UILabel alloc]init];
        _comfirmPwdLabel.font = [UIFont systemFontOfSize:loginChangeFont*LSCALE];
        _comfirmPwdLabel.text = @"确认密码：";
    }
    
    return _comfirmPwdLabel;
}

-(UILabel*) loginTitleLabel{
    
    if(!_loginTitleLabel){
        _loginTitleLabel = [[UILabel alloc]init];
        _loginTitleLabel.font = [UIFont systemFontOfSize:titleChangeFont*LSCALE];
        _loginTitleLabel.text = @"修改密码";
        _loginTitleLabel.textAlignment = NSTextAlignmentCenter;
        _loginTitleLabel.textColor = [UIColor whiteColor];
    }
    
    return _loginTitleLabel;
}

-(UILabel*) userNameLabel{
    
    if (!_userNameLabel) {
        _userNameLabel = [[UILabel alloc]init];
        _userNameLabel.font = [UIFont systemFontOfSize:loginChangeFont*LSCALE];
    }
    
    return _userNameLabel;
}
-(UITextField*) pwdTextField{
    
    if (!_pwdTextField) {
        _pwdTextField = [[UITextField alloc]init];
        _pwdTextField.font = [UIFont systemFontOfSize:loginChangeFont*LSCALE];
        _pwdTextField.placeholder = @"请输入密码";
        _pwdTextField.delegate = self;
        _pwdTextField.tag = 2;
        _pwdTextField.secureTextEntry = YES;
    }
    
    return _pwdTextField;
}
-(UITextField*) comfirmPwdTextField{
    
    if (!_comfirmPwdTextField) {
        _comfirmPwdTextField = [[UITextField alloc]init];
        _comfirmPwdTextField.font = [UIFont systemFontOfSize:loginChangeFont*LSCALE];
        _comfirmPwdTextField.placeholder = @"请确认密码";
        _comfirmPwdTextField.delegate = self;
        _comfirmPwdTextField.tag = 2;
        _comfirmPwdTextField.secureTextEntry = YES;
    }
    
    return _comfirmPwdTextField;
}
-(UIView*) contentView{
    
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.cornerRadius = 8;
    }
    return _contentView;
}

-(UIView*) topView{
    
    if (!_topView) {
        _topView = [[UIView alloc]init];
        _topView.backgroundColor = [ColorUtil titleColor];
        _topView.layer.cornerRadius = 8;
    }
    return _topView;
}

-(UIView*) middleView{
    
    if (!_middleView) {
        _middleView = [[UIView alloc]init];
        _middleView.backgroundColor = [UIColor whiteColor];
        
    }
    return _middleView;
}

-(UIView*) bottomView{
    
    if (!_bottomView) {
        _bottomView = [[UIView alloc]init];
        _bottomView.backgroundColor = [UIColor whiteColor];
        _bottomView.layer.cornerRadius = 8;
    }
    return _bottomView;
}

-(UIButton*) loginButton{
    
    if (!_loginButton) {
        
        _loginButton = [[UIButton alloc]init];
        _loginButton.layer.cornerRadius = 5;
        _loginButton.backgroundColor = [ColorUtil titleColor];
        _loginButton.tintColor = [UIColor whiteColor];
        [_loginButton setTitle:@"修改" forState:UIControlStateNormal];
        [_loginButton addTarget:self action:@selector(changeLogin) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}

-(ImageControl*) closeControl{
    if (!_closeControl) {
        _closeControl = [[ImageControl alloc]init];
        _closeControl.imageName = @"dismiss_img.png";
        [_closeControl addTarget:self action:@selector(removeSelf) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _closeControl;
}
-(UIControl*) backgroundControl{
    if (!_backgroundControl) {
        _backgroundControl = [[UIControl alloc]init];
        _backgroundControl.backgroundColor = [UIColor blackColor];
        _backgroundControl.alpha = 0.5;
        [_backgroundControl addTarget:self action:@selector(exitTextField) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _backgroundControl;
}
-(void) showMessage:(NSString*) message{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:  UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [alert dismissViewControllerAnimated:YES completion:nil];
    }]];
    
    [self presentViewController:alert animated:true completion:nil];
    
}
@end
